package com.alphacsp.theedge2012.data.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.*;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ViewAnimator;
import android.widget.ViewFlipper;
import com.alphacsp.theedge2012.R;
import com.alphacsp.theedge2012.data.entities.Session;
import com.alphacsp.theedge2012.ui.activities.SessionDetailsActivity;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 23/10/10
 * Time: 00:54
 */
public class SessionsAdapter extends ArrayAdapter<Session> {

    private static final int NORMAL_VIEW = 1;
    private static final int FLINGED_VIEW = 2;

    private int resourceId;

    private final LayoutInflater inflater;
    private GestureDetector gestureDetector;
    private final Context context;
    private boolean flingEvent;
    private boolean tapEvent;
    private boolean filngDirectionFromLeft;
    private int flingedPosition = -1;

    public SessionsAdapter(Context context, int textViewResourceId, List<Session> objects) {
        super(context, 0, objects);
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        resourceId = textViewResourceId;
        gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                final float startX = e1.getX();
                final float endX = e2.getX();
                final float deltaX = Math.abs(startX - endX);

                final float startY = e1.getY();
                final float endY = e2.getY();
                final float deltaY = Math.abs(startY - endY);

                if (endX > startX) {
                    filngDirectionFromLeft = true;
                } else {
                    filngDirectionFromLeft = false;
                }

                if (deltaX >= 40 && deltaY < 40) {
                    flingEvent = true;
                    return true;
                }
                return false;
            }

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                tapEvent = true;
                return true;
            }
        });
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final int itemViewType = getItemViewType(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.sessions_flipper, parent, false);
            convertView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    boolean result = gestureDetector.onTouchEvent(motionEvent);
                    if (flingEvent) {
                        final ViewFlipper viewFlipper = (ViewFlipper) view;
                        if (((ViewAnimator) view).getDisplayedChild() == 0) {
                            final ViewGroup group = (ViewGroup) view.getParent();
                            for (int i = 0; i < group.getChildCount(); i++) {
                                final ViewFlipper child = (ViewFlipper) group.getChildAt(i);
                                if (child.getDisplayedChild() != 0) {
                                    child.setDisplayedChild(0);
                                }
                            }
                        }
                        flingEvent = false;
                        if (filngDirectionFromLeft) {
                            viewFlipper.setInAnimation(inFromLeftAnimation());
                            viewFlipper.setOutAnimation(outToRightAnimation());
                        } else {
                            viewFlipper.setInAnimation(inFromRightAnimation());
                            viewFlipper.setOutAnimation(outToLeftAnimation());
                        }
                        viewFlipper.showNext();


                    } else if (tapEvent) {
                        tapEvent = false;
                        final Session session = getItem((Integer) view.getTag());
                        Intent intent = new Intent(context, SessionDetailsActivity.class);
                        intent.putExtra("sessionTopic", session.getTopic());
                        intent.putExtra("sessionPresenter", session.getPresenter());
                        intent.putExtra("sessionAbstract", session.getSessionAbstract());
                        context.startActivity(intent);
                    }
                    return result;
                }
            });
        }

        final TextView sessionName = (TextView) convertView.findViewById(R.id.session_name);
        final TextView speakerName = (TextView) convertView.findViewById(R.id.speaker_name);
        convertView.findViewById(R.id.profile_image).setTag(position);
        convertView.findViewById(R.id.session_image).setTag(position);


        Session session = getItem(position);
        sessionName.setText(session.getTopic());
        speakerName.setText(session.getPresenter());

        ((TextView) convertView.findViewById(R.id.session_name_flinged)).setText(session.getTopic());
        ((TextView) convertView.findViewById(R.id.speaker_name_flinged)).setText(session.getPresenter());

        convertView.setTag(position);
        return convertView;
    }

    private Animation inFromRightAnimation() {
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
        );
        inFromRight.setDuration(250);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
    }

    private Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
        );
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }

    private Animation inFromLeftAnimation() {
        Animation inFromLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
        );
        inFromLeft.setDuration(250);
        inFromLeft.setInterpolator(new AccelerateInterpolator());
        return inFromLeft;
    }

    private Animation outToRightAnimation() {
        Animation outtoRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
        );
        outtoRight.setDuration(250);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
    }
}




